package com.lecture.java8.datastructure;

/**
 * @author luyn
 * @version 1.0
 * @description
 * @date 2021/3/24 9:30
 */
public class Demo2 {

    public static void main(String[] args) {
        int[] a = {12,1,2,3,4,7,8,9,0,99};
        long start = System.currentTimeMillis();
        fx1(9, a, 1);
        long end = System.currentTimeMillis();
        System.out.println(end - start);


        long start1 = System.currentTimeMillis();
        fx2(9, a, 1);
        long end1 = System.currentTimeMillis();
        System.out.println(end1 - start1);
    }

    public static void fx1(int n, int[] a, double x) {
        double p = a[0];
        for (int i = 1; i <= n; i++) {
            p += a[i] * Math.pow(x, i);
        }
        System.out.println(p);
    }

    public static void fx2(int n, int[] a, double x) {
        double p = a[n];
        for (int i = n; i > 0 ; i--) {
            p = a[i - 1] + x*p;
        }
        System.out.println(p);
    }
}
